//
// Created by lz on 3/15/21.
//

#ifndef MYWEBLIB_NET_HTTPSERVER_H_
#define MYWEBLIB_NET_HTTPSERVER_H_

#include "net/TcpServer.h"

namespace lzweb
{
	namespace net
	{
		class HttpRequest;
		class HttpResponse;

		/// A simple embeddable HTTP server designed for report status of a program.
		/// It is not a fully HTTP 1.1 compliant server, but provides minimum features
		/// that can communicate with HttpClient and Web browser.
		/// It is synchronous, just like Java Servlet.
		class HttpServer : noncopyable
		{
		 public:
			using HttpCallback = std::function<void(const HttpRequest&, HttpResponse*)>;
			HttpServer(EventLoop* loop,
						const InetAddress& listenAddr,
						const string& name,
						TcpServer::Option option= TcpServer::kNoReusePort);
			EventLoop* getLoop()const { return server_.getLoop(); }

			///Not thread safe, callback be registered before calling start().

			void setHttpCallback(const HttpCallback& cb)
			{
				httpCallback_ =cb;
			}

			void setThreadNum(int numThreads)
			{
				server_.setThreadNum(numThreads);
			}

			void start();

		 private:

			void onConnection(const TcpConnectionPtr& conn);
			void onMessage(const TcpConnectionPtr& conn, Buffer* buf, TimeStamp receiveTime);
			void onRequest(const TcpConnectionPtr&, const HttpRequest&);

			TcpServer server_;
			HttpCallback httpCallback_;

		};
	} // namespace net

}// namespace lzweb


#endif //MYWEBLIB_NET_HTTPSERVER_H_
